package com.lam.common.components.easyexcel.handlers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.lam.common.utils.CollectionUtil;

/**
 * Excel下拉列表生成Handler
 * @author lam
 *
 */
public class DropDownWriteHandler implements SheetWriteHandler{
	
	protected int dropDownMaxRow = 300;
	protected Map<Integer, String[]> dropDownData = new HashMap<>();
	protected int rowCount;

	public DropDownWriteHandler() {}
	
	public DropDownWriteHandler(Map<Integer, String[]> dropDownData) {
		this(dropDownData, 0);
	}
	
	public DropDownWriteHandler(Map<Integer, String[]> dropDownData, int rowCount) {
		this.dropDownData = dropDownData;
		this.rowCount = rowCount;
	}

	@Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        
		if(CollectionUtil.isEmpty(dropDownData)) {
			return;
		}
        setValidationData(writeSheetHolder.getSheet(), dropDownData);
        
        //设置默认文本格式
        CellStyle textStyle = writeWorkbookHolder.getWorkbook().createCellStyle();
		DataFormat format = writeWorkbookHolder.getWorkbook().createDataFormat();
		textStyle.setDataFormat(format.getFormat("@"));
		for (int column = 0;column<12;column++) {
			writeSheetHolder.getSheet().setDefaultColumnStyle(column, textStyle);
		}
    }

	private void setValidationData(Sheet sheet, Map<Integer, String[]> mapDropDown) {
		
		int rowCount_ = rowCount > dropDownMaxRow ? rowCount : dropDownMaxRow;
        ///开始设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();
        for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
            /***起始行、终止行、起始列、终止列**/
            CellRangeAddressList addressList = new CellRangeAddressList(1, rowCount_, entry.getKey(), entry.getKey());
            /***设置下拉框数据**/
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            /***处理Excel兼容性问题**/
            if (dataValidation instanceof XSSFDataValidation) {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            } else {
                dataValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(dataValidation);
        }
	}

	public Map<Integer, String[]> getDropDownData() {
		return dropDownData;
	}

	public DropDownWriteHandler setDropDownData(Map<Integer, String[]> dropDownData) {
		this.dropDownData = dropDownData;
		return this;
	}
	
	public DropDownWriteHandler dropDownData(Map<Integer, List<String>> dropDownData) {
		Map<Integer, String[]> dropDownData0 = new HashMap<>();
		for (Entry<Integer, List<String>> entry : dropDownData.entrySet()) {
			dropDownData0.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()]));
		}
		this.dropDownData = dropDownData0;
		return this;
	}

	public int getRowCount() {
		return rowCount;
	}

	public DropDownWriteHandler setRowCount(int rowCount) {
		this.rowCount = rowCount;
		return this;
	}

	@Override
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
	}
	
}
